package ${package.Controller}.${cfg.moduleName};

import ${cfg.basePath}.domain.${cfg.moduleName}.${entity};
import ${cfg.basePath}.result.PageList;
import ${cfg.basePath}.exception.MyException;
import ${cfg.basePath}.service.${cfg.moduleName}.${entity}Service;
import ${cfg.basePath}.query.${cfg.moduleName}.${entity}Query;
import ${cfg.basePath}.annotation.CheckPermission;
import ${cfg.basePath}.result.JSONResult;
import ${cfg.basePath}.annotation.MyLog;
import ${cfg.basePath}.enums.BusinessType;
import ${cfg.basePath}.controller.base.BaseController;
import ${cfg.basePath}.utils.DateUtils;
import ${cfg.basePath}.utils.poi.ExcelUtil;
import ${cfg.basePath}.constants.ErrorCode;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;

/**
 * @desc ${table.comment!} API接口
 * @author ${author}
 * @date ${date}
 */
@RestController
@RequestMapping("/${cfg.entityName}")
public class ${entity}Controller extends BaseController{

    @Resource
    public ${entity}Service ${cfg.entityName}Service;

    /**
     * 新增数据到【${table.comment!}】
     * @date ${date}
     */
    @PostMapping(value="/save")
    @CheckPermission(per = "${cfg.entityName}:save")
    @MyLog(title = "新增数据到【${table.comment!}】", businessType = BusinessType.INSERT)
    public JSONResult save(@RequestBody ${entity} ${cfg.entityName}){
        ${cfg.entityName}.setCreateTime(DateUtils.getCurrentLocalDateTime());
        ${cfg.entityName}.setCreateUserId(getUserId());
        ${cfg.entityName}.setCreateUserName(getUserName());
        ${cfg.entityName}.setUpdateTime(DateUtils.getCurrentLocalDateTime());
        ${cfg.entityName}.setUpdateUserId(getUserId());
        ${cfg.entityName}.setUpdateUserName(getUserName());
        ${cfg.entityName}Service.save(${cfg.entityName});
        return JSONResult.success(true);
    }

    /**
    * 删除【${table.comment!}】数据
    * @date ${date}
    */
    @PostMapping(value="/batchDelete")
    @MyLog(title = "删除【${table.comment!}】数据", businessType = BusinessType.DELETE)
    @CheckPermission(per = "${cfg.entityName}:deleteBatch")
    public JSONResult batchDelete(@RequestBody ${entity}Query query){
        if(query.getIds() == null){
            throw new MyException(ErrorCode.COMMON_CODE_2001);
        }

        //删除业务数据时，阿里云OSS中的图片也需要一起删掉，节省空间
        //List<String> ossFullPathList = new ArrayList<>();
        //List<${entity}> listData = ${cfg.entityName}Mapper.selectBatchIds(Arrays.asList(query.getIds()));
        //if(listData != null && listData.size() > 0){
        //for(int i=0; i<listData.size(); i++){
        //ossFullPathList.add(listData.get(i).getFaImg());
        //}
        //}
        //fileUploadUtils.deleteFileFromOss(ossFullPathList);

        //删除数据库数据
        ${cfg.entityName}Service.removeByIds(Arrays.asList(query.getIds()));
        return JSONResult.success(true);
    }

    /**
     * 修改【${table.comment!}】表数据
     * @date ${date}
     */
    @PostMapping(value="/update")
    @MyLog(title = "修改【${table.comment!}】表数据", businessType = BusinessType.UPDATE)
    @CheckPermission(per = "${cfg.entityName}:update")
    public JSONResult update(@RequestBody ${entity} ${cfg.entityName}){
        ${cfg.entityName}.setUpdateTime(DateUtils.getCurrentLocalDateTime());
        ${cfg.entityName}.setUpdateUserId(getUserId());
        ${cfg.entityName}.setUpdateUserName(getUserName());
        ${cfg.entityName}Service.updateById(${cfg.entityName});
        return JSONResult.success(true);
    }

    /**
     * 查询【${table.comment!}】数据（分页）
     * @param query 查询对象
     * @return PageList 分页对象
     * @date ${date}
     */
    @PostMapping(value = "/pagelist")
    @MyLog(title = "查询【${table.comment!}】数据（分页）", businessType = BusinessType.QUERY)
    @CheckPermission(per = "${cfg.entityName}:pagelist")
    public JSONResult pagelist(@RequestBody ${entity}Query query){
        Page<${entity}> page = ${cfg.entityName}Service.selectMySqlPage(query);
        return JSONResult.success(new PageList<>(page.getTotal(), page.getRecords()));
    }

    /**
    * 查询【${table.comment!}】数据（不分页）
    * @date ${date}
    */
    @GetMapping(value = "/list")
    @MyLog(title = "查询【${table.comment!}】数据（不分页）", businessType = BusinessType.QUERY)
    public JSONResult list(){
        QueryWrapper<${entity}> queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("id");//根据ID降序排序，最新的数据展示在最上面
        List<${entity}> list = ${cfg.entityName}Service.list(queryWrapper);
        return JSONResult.success(list);
    }

    /**
    * 根据ID查询【${table.comment!}】数据
    * @date ${date}
    */
    @GetMapping(value = "/getDataById/{id}")
    @MyLog(title = "根据ID查询【${table.comment!}】数据", businessType = BusinessType.QUERY)
    public JSONResult getDataById(@PathVariable("id")Long id){
        return JSONResult.success(${cfg.entityName}Service.getById(id));
    }

    /**
     * 根据 QueryWrapper 对象查询【${table.comment!}】数据
     * @date ${date}
    */
    @GetMapping(value = "/queryWrapper/{objName}")
    public JSONResult queryWrapper(@PathVariable("objName") String objName){
        QueryWrapper<${entity}> queryWrapper = new QueryWrapper();
        queryWrapper.eq("", objName);
        ${entity} one = ${cfg.entityName}Service.getOne(queryWrapper);
        return JSONResult.success(one);
    }

    /**
     * 【${table.comment!}】数据导出Excel
    */
    @PostMapping(value="/exportExcelFile")
    @CheckPermission(per = "${cfg.entityName}:exportExcelFile")
    @MyLog(title = "【${table.comment!}】数据导出Excel", businessType = BusinessType.EXPORT)
    public void exportExcelFile(HttpServletResponse response, ${entity}Query query){
        List<${entity}> list = ${cfg.entityName}Service.selectExportExcelData(query);
        ExcelUtil<${entity}> util = new ExcelUtil<>(${entity}.class);
        util.exportExcel(response, list, "${table.comment!}");
    }

    /**
     * xxx操作
    */
    @PostMapping(value="/xxxHandle")
    @MyLog(title = "xxxxxxxxxx", businessType = BusinessType.UPDATE)
    @CheckPermission(per = "xxxxxx:xxxxxx")
    public JSONResult xxxHandle(@RequestBody ${entity} ${cfg.entityName}){
        ${entity} obj = ${cfg.entityName}Service.getById(${cfg.entityName}.getId());
        if(obj == null){
            throw new MyException(ErrorCode.COMMON_CODE_2001);
        }
        //做其他操作
        ${cfg.entityName}Service.updateById(obj);
        return JSONResult.success(true);
    }

    /***********************************************************************************
    以上代码是自动生成的
    自己写的代码放在下面
    ***********************************************************************************/





}